package com.fr.chicken.config;


import com.fr.chicken.controller.scheduletask.ChickenScheduleTask;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

    /**
     * attention:
     * Details：配置定时任务
     */
    @Bean(name = "generateEggJobDetail")
    public MethodInvokingJobDetailFactoryBean generateEggJobDetail(ChickenScheduleTask task) {// ScheduleTask为需要执行的任务
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        jobDetail.setConcurrent(true);// 是否并发执行
        jobDetail.setTargetObject(task);
        /*
         * 添加需要执行的方法
         * 通过这几个配置，告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的需要执行方法
         */
        jobDetail.setTargetMethod("generateEgg");
        return jobDetail;
    }
    /**
     * attention:
     * Details：配置定时任务的触发器，也就是什么时候触发执行定时任务
     */
    @Bean(name = "generateEggJobTrigger")
    public CronTriggerFactoryBean generateEggJobTrigger(JobDetail generateEggJobDetail) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(generateEggJobDetail);
        tigger.setCronExpression("0 */1 * * * ?");// 初始时的cron表达式  ，每1分钟执行一次
        return tigger;

    }

    /**
     * attention:
     * Details：配置定时任务
     */
    @Bean(name = "chickenDieJobDetail")
    public MethodInvokingJobDetailFactoryBean chickenDieJobDetail(ChickenScheduleTask task) {// ScheduleTask为需要执行的任务
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        jobDetail.setConcurrent(true);// 是否并发执行
        jobDetail.setTargetObject(task);
        /*
         * 添加需要执行的方法
         * 通过这几个配置，告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的需要执行方法
         */
        jobDetail.setTargetMethod("chickenDie");
        return jobDetail;
    }
    /**
     * attention:
     * Details：配置定时任务的触发器，也就是什么时候触发执行定时任务
     */
    @Bean(name = "chickenDieJobTrigger")
    public CronTriggerFactoryBean chickenDieJobTrigger(JobDetail chickenDieJobDetail) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(chickenDieJobDetail);
        tigger.setCronExpression("0 */1 * * * ?");// 初始时的cron表达式  ，每1分钟执行一次
        return tigger;

    }

    /**
     * attention:
     * Details：配置定时任务
     */
    @Bean(name = "generateFaecesJobDetail")
    public MethodInvokingJobDetailFactoryBean generateFaecesJobDetail(ChickenScheduleTask task) {// ScheduleTask为需要执行的任务
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        jobDetail.setConcurrent(true);// 是否并发执行
        jobDetail.setTargetObject(task);
        /*
         * 添加需要执行的方法
         * 通过这几个配置，告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的需要执行方法
         */
        jobDetail.setTargetMethod("generateFaeces");
        return jobDetail;
    }
    /**
     * attention:
     * Details：配置定时任务的触发器，也就是什么时候触发执行定时任务
     */
    @Bean(name = "generateFaecesJobTrigger")
    public CronTriggerFactoryBean generateFaecesJobTrigger(JobDetail generateFaecesJobDetail) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(generateFaecesJobDetail);
        tigger.setCronExpression("0 */1 * * * ?");// 初始时的cron表达式  ，每1分钟执行一次
        return tigger;

    }

    /**
     * attention:
     * Details：配置定时任务
     */
    @Bean(name = "generateFeatherJobDetail")
    public MethodInvokingJobDetailFactoryBean generateFeatherJobDetail(ChickenScheduleTask task) {// ScheduleTask为需要执行的任务
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        jobDetail.setConcurrent(true);// 是否并发执行
        jobDetail.setTargetObject(task);
        /*
         * 添加需要执行的方法
         * 通过这几个配置，告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的需要执行方法
         */
        jobDetail.setTargetMethod("generateFeather");
        return jobDetail;
    }
    /**
     * attention:
     * Details：配置定时任务的触发器，也就是什么时候触发执行定时任务
     */
    @Bean(name = "generateFeatherJobTrigger")
    public CronTriggerFactoryBean generateFeatherJobTrigger(JobDetail generateFeatherJobDetail) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(generateFeatherJobDetail);
        tigger.setCronExpression("0 */1 * * * ?");// 初始时的cron表达式  ，每1分钟执行一次
        return tigger;

    }

    @Bean(name = "deleteMessageJobDetail")
    public MethodInvokingJobDetailFactoryBean deleteMessageJobDetail(ChickenScheduleTask task) {// ScheduleTask为需要执行的任务
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        jobDetail.setConcurrent(true);// 是否并发执行
        jobDetail.setTargetObject(task);
        /*
         * 添加需要执行的方法
         * 通过这几个配置，告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的需要执行方法
         */
        jobDetail.setTargetMethod("deleteMessage");
        return jobDetail;
    }
    /**
     * attention:
     * Details：配置定时任务的触发器，也就是什么时候触发执行定时任务
     */
    @Bean(name = "deleteMessageJobTrigger")
    public CronTriggerFactoryBean deleteMessageJobTrigger(JobDetail deleteMessageJobDetail) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(deleteMessageJobDetail);
        tigger.setCronExpression("0 */1 * * * ?");// 初始时的cron表达式  ，每1分钟执行一次
        return tigger;

    }
    /**
     * attention:
     * Details：定义quartz调度工厂
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger generateEggJobTrigger,Trigger chickenDieJobTrigger,Trigger generateFaecesJobTrigger,Trigger generateFeatherJobTrigger,Trigger deleteMessageJobTrigger) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        bean.setOverwriteExistingJobs(true);
        // 延时启动，应用启动1秒后
        bean.setStartupDelay(0);
        // 注册触发器
        bean.setTriggers(generateEggJobTrigger,chickenDieJobTrigger,generateFaecesJobTrigger,generateFeatherJobTrigger,deleteMessageJobTrigger);
        return bean;
    }
}
